/**
 * Created by lenovo on 2017/2/21.
 */
'use strict'
const path=require('path');
const fs=require('fs');
//获取服务器

const getMongo=require(path.join(__dirname,'../tools/getdb.js'))
//获取登录
exports.getLogin=(req,res)=>{
    fs.readFile(path.join(__dirname,'../views/login.html'),(err,data)=>{
        if(err){
            console.log(err);
            return;
        }
        res.setHeader('Content-Type','text/html;charset=utf-8');
        res.end(data);
    })
}
//登录到管理系统
exports.login=(req,res)=>{
    const parms=req.body;
    //获取数据库的用户信息，如果有就登录，没有就提示错误
    //const  MongoClient =mongodb.MongoClient;
    //const url='mongodb://localhost:27017/studentsystms';
//连接数据库到客户端
    getMongo.findOne('account',parms,(doc)=>{
        res.setHeader("Content-Type","text/html;charset=utf-8")
        if(doc==null){
            //console.log(doc+'????');
            res.end("<script>alert('用户名或密码错误');window.location.href='/account/login'</script>");

        }else{
            console.log(doc);
            //console.log(parms);
            //console.log(parms.username);
            //console.log('内存');
            //console.log(req.session);
            req.session.username=parms.username;

            res.end("<script>window.location.href='/student/info'</script>");
            //req.session.username=parms.username

            //console.log(req.session);
            //console.log(req.session.username);
            //console.log(doc);
        }
    })
    //MongoClient.connect(url,(err,db)=>{
    //    db.collection('account').findOne(parms,(err,doc)=>{
    //        if(err){
    //            console.log(err);
    //            return;
    //        }
    //        console.log(parms);
    //        res.setHeader("Content-Type","text/html;charset=utf-8")
    //        if(doc==null){
    //            console.log(doc);
    //            res.end("<script>alert('用户名或密码错误');window.location.href='/account/login/'</script>");
    //
    //        }else{
    //            res.end("<script>window.location.href='/student/info/'</script>");
    //
    //            console.log(doc);
    //        }
    //    })
    //})
}
//获取注册界面
exports.getRegister=(req,res)=>{
    fs.readFile(path.join(__dirname,'../views/register.html'),(err,data)=>{
        if(err){
            console.log(err);
            return;
        }
        res.setHeader('Content-Type','text/html;charset=utf-8');
        res.end(data);
    })
}
//注册逻辑
exports.register=(req,res)=>{
    const params=req.body;
    console.log(params);
    //getMongo.findOne('account',params,(doc)=>{
    //
    //    if(doc.username==params.username){
    //        res.setHeader("Content-Type","text/html;charset=utf-8");
    //        res.end("<script>alert('用户名重复')</script>")
    //    }else{

            //res.end('ok');
            getMongo.add('account',params,(insertSuccess)=>{
                const result = {status:1,message:"注册成功"}
                if (!insertSuccess) {
                    result.status = 0;
                    result.message = "注册失败"
                }
                res.json(result)
            })
    //    }
    //})
}
//退出登录
exports.logout=(req,res)=>{
    req.session.username=null;
    res.setHeader("Content-Type","text/html;charset=utf-8");
    res.end("<script>window.location.href='/account/login'</script>")
}